package com.gaoyi.crm.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;

import com.gaoyi.crm.R;
import com.gaoyi.crm.util.DensityUtil;

public class KeyValueTextView extends View {

    private String mKey;
    private String[] mValue;
    private float mKeySize, mValueSize, mValueY, mKeyBaseline;
    private int mKeyColor, mValueColor, mBorderColor, mMaxValueWidth;
    private Paint mKetPaint, mBorderPaint;
    private TextPaint mValuePaint;
    private int mWidth, mHeight;
    private StringBuffer mValueStr;
    private StaticLayout mStaticLayout;


    public void setValue(String[] value) {
        this.mValue = value;
        handleValue();
    }

    private void handleValue() {
        mValueStr = new StringBuffer(mValue[0]);
        String maxStr = mValue[0];
        if (mValue.length > 1) {
            int maxLength = maxStr.length();
            for (int i = 1; i < mValue.length; i++) {
                String s = mValue[i];
                int length = s.length();
                mValueStr.append("\n").append(s);
                if (length > maxLength) {
                    maxLength = length;
                    maxStr = s;
                }
            }
            mValueY = 0;
        } else {
            Paint.FontMetrics fm = mValuePaint.getFontMetrics();
            mValueY = - fm.descent - 2 * fm.ascent - 10;// 右边底部显示
//            mValueY = mHeight - fm.descent - 2 * fm.ascent - 10;// 右边底部显示
        }
        mMaxValueWidth = (int) mValuePaint.measureText(maxStr);
        StaticLayout.Builder slb = StaticLayout.Builder.obtain(mValueStr, 0, mValueStr.length(), mValuePaint, mMaxValueWidth);
        mStaticLayout = slb.build();
        invalidate();
    }

    public KeyValueTextView(Context context) {
        super(context);
        init(context, null);
    }

    public KeyValueTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public KeyValueTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.KeyValueTextView);
        mKey = ta.getString(R.styleable.KeyValueTextView_key);
        mKeySize = ta.getDimension(R.styleable.KeyValueTextView_key_textSize, DensityUtil.getSPValue(context, 18));
        mValueSize = ta.getDimension(R.styleable.KeyValueTextView_value_textSize, DensityUtil.getSPValue(context, 12));
        mKeyColor = ta.getColor(R.styleable.KeyValueTextView_key_color, Color.BLACK);
        mValueColor = ta.getColor(R.styleable.KeyValueTextView_value_color, ResourcesCompat.getColor(getResources(),R.color.color_333,null));
        mBorderColor = ta.getColor(R.styleable.KeyValueTextView_border_color, ResourcesCompat.getColor(getResources(),R.color.color_70,null));
        ta.recycle();
        mKetPaint = new Paint();
        mKetPaint.setColor(mKeyColor);
        mKetPaint.setTextSize(mKeySize);
        mValuePaint = new TextPaint();
        mValuePaint.setColor(mValueColor);
        mValuePaint.setTextSize(mValueSize);
        mBorderPaint = new Paint();
        mBorderPaint.setColor(mBorderColor);
        mBorderPaint.setStrokeWidth(DensityUtil.dp2px(1));
        mBorderPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(0, mHeight, mWidth, mHeight, mBorderPaint);
        canvas.save();
        if (mKey == null) return;
        Paint.FontMetrics fm = mKetPaint.getFontMetrics();
        mKeyBaseline = (mHeight - (fm.descent - fm.ascent)) / 2 - fm.ascent;// 左边居中显示
        canvas.drawText(mKey, getPaddingStart(), mKeyBaseline, mKetPaint);
        if (mValue == null) return;
        canvas.save();
        canvas.translate(mWidth - mMaxValueWidth, mValueY);
        mStaticLayout.draw(canvas);
        canvas.restore();
    }
}
